<script setup>
import { ref } from "vue"
import { ElMessage } from 'element-plus';
import { useRouter, useRoute } from "vue-router";
import { login } from "@/api/user";
import Cookies from "js-cookie";
import store from "@/store";
import { encrypt, decrypt } from "@/utils/jsencrypt";
const router = useRouter();
const route = useRoute();

// 表单数据
const form_login = ref({
  username: '',
  password: '',
  rememberMe: false
});

// 登录处理
const handleLogin = async () => {
  if (!form_login.value.username.trim()) {
    ElMessage.error('请输入用户名');
    return;
  }

  if (!form_login.value.password) {
    ElMessage.error('请输入密码');
    return;
  }
  if (form_login.value.rememberMe) {
    Cookies.set("username", form_login.value.username, { expires: 30 });
    Cookies.set("password", encrypt(form_login.value.password), { expires: 30 });
    Cookies.set("rememberMe", form_login.value.rememberMe, { expires: 30 });
  } else {
    // 否则移除
    Cookies.remove("username");
    Cookies.remove("password");
    Cookies.remove("rememberMe");
  }


  try {
    const res = await login({
      username: form_login.value.username,
      password: form_login.value.password,
      rolekey: 'admin'

    });
    if (res) {
      const token = res.token
      const currentUser = res.currentUser;
      console.log("token=" + token);

      store.commit('SET_TOKEN', token)
      store.commit("SET_USERINFO", currentUser);

      router.push("/system/user")
      ElMessage.success("登录成功")

      localStorage.setItem("token", res.token);

    } else {
      ElMessage.error('登录失败');
    }
  } catch (error) {
    ElMessage.error('登录失败');
    console.error('登录错误:', error);
  }
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  form_login.value = {
    username: username === undefined ? form_login.value.username : username,
    password: password === undefined ? form_login.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  };
}
getCookie();
</script>

<template>
  <div class="login-container">
    <div class="login-form">
      <h2 class="login-title">智慧养老管理平台</h2>
      <el-form :model="form_login" :label-width="60" @submit="handleLogin">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form_login.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form_login.password" type="password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
           <!-- <el-row>
          <el-checkbox v-model="form_login.rememberMe" style="margin:0px 0px 0px 0px;">记住密码</el-checkbox>
      </el-row> -->

      </el-form>
      <div class="button-wrapper">
            <el-button class="login-button" type="primary" @click="handleLogin">登录</el-button>
          </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@use '@/assets/scss/abstracts/variables' as *;
@use '@/assets/scss/abstracts/mixins' as *;

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-form {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
  }
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #3dadee;
  font-size: 24px;
  font-weight: 600;
}

.login-actions {
  margin-top: 30px;
}

.button-wrapper {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}


.login-button {
  width: 180px; // 设置固定宽度使按钮居中效果更明显
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  background-color: #27a7f1;
  border: none;
  transition: all 0.3s ease;

  &:hover {
    opacity: 0.9;
    transform: scale(1.02);
  }

  &:active {
    transform: scale(0.98);
  }
}
</style>